/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.netplay.server;

import java.util.HashSet;
import java.util.Set;
import sirtet.netplay.NetworkPlayer;

/**
 * A server base which provides built-in chain-of-call as well as function slots
 * that can be overriden to handle events
 * @author guangwei.zhu
 */
public class ServerObject extends Object {

    protected Set<ServerObject> handlers = new HashSet<ServerObject>();

    public void addHandler(ServerObject handler) {
        handlers.add(handler);
    }

    public void removeHandler(ServerObject handler) {
        handlers.remove(handler);
    }

    protected void playerArrived(NetworkPlayer player) {  // Must be overridden
        throw new UnsupportedOperationException("This server object cannot handle any player.");
    }

    protected void notifyPlayerArrived(NetworkPlayer player) {
        for (ServerObject handler : handlers) {
            try {
                handler.playerArrived(player);
            } catch (Exception ex) {
            }
        }
    }
}
